# -*- coding: utf-8 -*-
from houseone.items import HouseoneItem
from scrapy.loader import ItemLoader
from scrapy.loader.processors import MapCompose, Join
from urllib.parse import urlparse
import datetime
import socket
import scrapy


class BasicSpider(scrapy.Spider):
    name = 'basic'
    allowed_domains = ['localhost']
    start_urls = ['http://localhost:9312/properties/property_000000.html']

    def parse(self, response):
        '''
        @url http://localhost:9312/properties/property_000000.html
        @returns items 1
        @scrapes title price description address image_urls
        @scrapes url project spider server date
        :param response:
        :return:
        '''
        l = ItemLoader(item=HouseoneItem(), response=response)

        l.add_xpath('title', '//*[@itemprop = "name"][1]/text()')
        l.add_xpath('price', '//*[@itemprop = "price"][1]/text()', MapCompose(lambda i: i.replace(',', ''), float),
                    re='[,.0-9]+')
        l.add_xpath('description', '//*[@itemprop = "description"][1]/text()')
        l.add_xpath('address', '//*[@itemtype = "http://schema.org/Place"][1]/text()')
        l.add_xpath('image_urls', '//*[@itemprop = "image"][1]/@src')

        l.add_value('url', response.url)
        l.add_value('project', self.settings.get('BOT_NAME'))
        l.add_value('spider', self.name)
        l.add_value('server', socket)
        l.add_value('date', datetime.datetime.now())

        return l.load_item()
